package com.visikard.ui.activity;


import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.visikard.application.Constant;
import com.visikard.item.ItemTransactionHistory;
import com.visikard.utility.UILImageUtils;
import com.visikard.ui.R;
import com.visikard.utility.BitmapUtils;
import com.visikard.utility.Utils;

public class TransactionDetailActivity extends BaseLoginCompleteActivity implements OnClickListener {

	private Context mContext;
	private ImageView imgViewTarget, imgBack;
	private Button btnBack;
	private RelativeLayout parent;
	private LinearLayout seperateGK;
	private TextView audienceName, address, transactionTime, gender, age, amount, redeemed, remain;
	private ItemTransactionHistory itemTransHis;
	
	public static final String EXTRA_ITEM_TARGET = "item_target_transaction_detail";
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.transaction_detail_activity);
		
		mContext = this;
		itemTransHis = (ItemTransactionHistory) getIntent().getParcelableExtra(EXTRA_ITEM_TARGET);
		initView();
	}
	
	private void initView() {
		parent = (RelativeLayout) findViewById(R.id.wrapper_global_transaction_detail);
		
		seperateGK = (LinearLayout) findViewById(R.id.separate_info_gk_transaction_detail);
		imgViewTarget = (ImageView) findViewById(R.id.img_target_transaction_detail);
		audienceName = (TextView) findViewById(R.id.val_audience_name_transaction_detail);
		address = (TextView) findViewById(R.id.val_address_transaction_detail);
		amount = (TextView) findViewById(R.id.val_amount_transaction_detail);
		redeemed = (TextView) findViewById(R.id.val_redeemed_transaction_detail);
		remain = (TextView) findViewById(R.id.val_remaining_transaction_detail);
		transactionTime = (TextView) findViewById(R.id.val_transaction_time_transaction_detail);
		gender = (TextView) findViewById(R.id.val_gender_transaction_detail);
		age = (TextView) findViewById(R.id.val_age_transaction_detail);
		
		imgBack = (ImageView) findViewById(R.id.img_back_transaction_detail);
		
		// Register click event
		imgBack.setOnClickListener(this);
		
		//Set font for Text view
		Utils.overrideFonts(parent, Constant.FONT_GOTHAM_BOOK);
		
		//Gone if transaction isn't belong giftkard
		if(itemTransHis.getCategoryCard() == 2) {
			UILImageUtils.loadImage(imgViewTarget, itemTransHis.getImageGiftKard(), 1, BitmapUtils.ROUND_ALL, 0, 0, 0, 0,0);
			double amount_real = 0;
			try{
				if(!itemTransHis.getRedemptionValue().isEmpty() && !itemTransHis.getRemainingValue().isEmpty())
					amount_real = Double.parseDouble(itemTransHis.getRedemptionValue()) + Double.parseDouble(itemTransHis.getRemainingValue());
			} catch (Exception e) {
				e.printStackTrace();
			}
			String _amount = String.format(getString(R.string.transaction_detail_amount_text), "$" + String.valueOf(amount_real) + ",");
			amount.setText(Html.fromHtml(_amount));
			
			String _redeemed = String.format(getString(R.string.transaction_detail_redeemed_text), "$" + itemTransHis.getRedemptionValue() + ", ");
			redeemed.setText(Html.fromHtml(_redeemed));
			
			String _remain = String.format(getString(R.string.transaction_detail_remaining_text), "$" + itemTransHis.getRemainingValue());
			remain.setText(Html.fromHtml(_remain));
		} else {
			seperateGK.setVisibility(View.GONE);
			UILImageUtils.loadImage(imgViewTarget, itemTransHis.getImageGiftKard(), 1, BitmapUtils.ROUND_NONE, 0, 0, 0, 0,0);
		}
		// Set value
		
		
		String _address = String.format(getString(R.string.transaction_detail_address_text), itemTransHis.getAddress());
		address.setText(Html.fromHtml(_address));
		
		String _audienceName = String.format(getString(R.string.transaction_detail_audience_name_text), itemTransHis.getUsername());
		audienceName.setText(Html.fromHtml(_audienceName));
		
		//String _transactionTime = "Oct 16 2014, 14:30";
		String _tempDate = Utils.wrapperFormatDate(itemTransHis.getDateTimeWithTimezone(), "MM/dd/yyyy HH:mm:ss", "MMM dd yyyy, HH:mm");
		String _transactionTime = String.format(getString(R.string.transaction_detail_transaction_time_text), _tempDate);
		transactionTime.setText(Html.fromHtml(_transactionTime)); 
		
		String _gender = String.format(getString(R.string.transaction_detail_gender_text), itemTransHis.getGender());
		gender.setText(Html.fromHtml(_gender));
		
		String _age = String.format(getString(R.string.transaction_detail_age_text), itemTransHis.getAge());
		age.setText(Html.fromHtml(_age));
		
		
		//return parent;
	}

		
	@Override
	public void onPause() {
		super.onPause();
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}
	
	@Override
	public void onBackPressed() {
		finish();
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.img_back_transaction_detail:
				finish();
				break;
			default:
				break;
		}

	}
	

}
